<?php
namespace src;
use classlib\acl;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
use classlib\validator\auditor;
class Caccount extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$lang  = C\lang::inst('adm');
		$dlang = lang::inst('dd');
		$view->assign('sex', $dlang->get('uc_sex'));
		$view->assign('yes2no', $dlang->get('sh0007'));
		$view->assign('status', $dlang->get('uc_status'));
		$view->assign('origin', $dlang->get('uc_origin'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/account.ext.js');
	}
	
	/**
	 * @desc    禁用/解禁 平台的会员账号信息
	 * @doc     禁用解禁
	 * @access  需要授权 
	 */
	public function dostatus() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids    = auditor::intstr2arr('ids');
		$lang   = lang::inst('adm');
		if(empty($ids)) {
			$view->assign_error('1', $lang->get('common', '40001'));
		}
		$remark = input::get_post('remark');
		$remark = trim($remark);
		if(empty($remark) || strlen($remark) < 3) {
			$view->assign_error('logic', $lang->get('logic', '60001'));
		}
		$user   = acl::inst()->user;
		$opuser = ($user)? $user['nick'].'['.$user['account'].']':'[匿名]';
		$uorm   = C\model::load('pro.pl.userinfo');
		$field  = array('status'=>2);
		foreach ($ids as $userid) {
			$user = $uorm->get_one($userid);
			if (!$user) {
				continue;
			}
			$field['status'] = ($user['status'] == 2)? 1:2;
			$uorm->save($userid, $field);
			$status= ($field['status'] == 1)? 'Enabled':'Disabled';
			$msg   = sprintf('disabled/enabled-user:%s/%s[%d] %s remark:%s', $user['nickname'],
				$user['email'], $userid, $status, $remark);
			Cadmin::log2db($msg);
			acl::inst('ACLweb')->ss($userid, C\rand_str(8));
		}
		$view->display();
	}
	
	/**
	 * @desc    设置客服号码或者机器人
	 * @doc     客服/机器人
	 * @access  需要授权
	 */
	public function dosuper() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids    = auditor::intstr2arr('s');
		$robots = (int)input::get_post('isrobots');
		$rorm   = C\model::load('pro.inc.admin_user');
		$robots = ($robots > 0)? 1:0;
		foreach ($ids as $userid) {
			$admin = $rorm->get_one($userid);
			if (!$admin) {
				$fields = array('userid'=>$userid, 'isrobots'=>$robots);
				$rorm->new_one($fields);
			} else if ($admin['isrobots'] != $robots) {
				$rorm->save($userid, array('isrobots'=>$robots));
			} else {
				$rorm->delete($userid);
			}
		}
		$view->display();
	}
	
	/**
	 * 获取用户资料列表
	 */
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$query  = input::post('query');
		if ($query && !empty($query)) {
			$where[] = array('(');
			$where[] = array('nickname', '%'.$query.'%', \classlib\orm::OP_LIKE);
			$where[] = array('email', '%'.$query.'%', \classlib\orm::OP_LIKE, \classlib\orm::OP_OR);
			$where[] = array(')');
		}
		$status = input::post('status');
		is_numeric($status) && $where['status']    = $status;
		$origin = input::post('origin');
		is_numeric($origin) && $where['regorigin'] = $origin;
		$sdate  = input::post('sdate');
		if ($sdate) {
			$where[] = array('regtime', strtotime($sdate), \classlib\orm::OP_GT);
		}
		$edate  = input::post('edate');
		if ($edate) {
			$where[] = array('regtime', strtotime($edate), \classlib\orm::OP_LT);
		}
		$uorm   = C\model::load('pro.pl.userinfo');
		$fields = $uorm->get_fields();
		unset($fields['loginpw'], $fields['token']);
		$form   = C\model::load('pro.pl.finance');
		$rorm   = C\model::load('pro.inc.admin_user');
		$total  = $uorm->get_total($where);
		$list   = array();
		if ($total > 0) {
			$fields = implode(',', array_keys($fields));
			$list = $uorm->get_list($start, $limit, $where, $fields, $sort);
			foreach ($list as &$item) {
				$finance = $form->get_one($item['userid']);
				$admin   = $rorm->get_one($item['userid']);
				$item['issuper'] = 0;
				$item['isrobot'] = 0;
				if ($admin) {
					$item['issuper'] = ($admin['isrobots'] > 0)? 0:1;
					$item['isrobot'] = ($admin['isrobots'] > 0)? 1:0;
				}
				$item['regtime'] = date('Y-m-d H:i:s', $item['regtime']);
				$item['lasttime']= date('Y-m-d H:i:s', $item['lasttime']);
				$item['money']    = $finance['money'] / 100;
				$item['totalcoin']= $finance['totalcoin'];
				$item['conscoin'] = $finance['conscoin'];
				$item['lastcoin'] = $finance['lastcoin'];
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}